/*
 * @(#)SpecificationBehaviorConsistencyChecker.java   09/10/12
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.model.consistencyChecker;

import fr.ema.arch3d.model.architecture.specification.ComponentRole;
import fr.ema.arch3d.model.architecture.specification.Connection;
import fr.ema.arch3d.model.architecture.specification.Specification;

import org.objectweb.fractal.bpc.staticchecker.Binding;
import org.objectweb.fractal.bpc.staticchecker.Checker;
import org.objectweb.fractal.bpc.staticchecker.CheckingResult;
import org.objectweb.fractal.bpc.staticchecker.FrameInstance;
import org.objectweb.fractal.bpc.staticchecker.InterfaceInstance;
import org.objectweb.fractal.bpc.staticchecker.preprocessor.Debug;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/18
 * @author         Huaxi (Yulin) ZHANG
 */
public class SpecificationBehaviorConsistencyChecker {

    /**
     * Method description
     *
     * @param spec  the specification which need to be checked
     */
    public static void check(Specification spec) {

        // set the level of composition of specification
        // the initial level should be 2
        // the super component contains nothing
        Debug.setLevel(2);

        /** super component */
        FrameInstance superComponent = new FrameInstance(null, new InterfaceInstance[0], "NULL", 0);

        /** the components in this specification */
        FrameInstance[] fis = new FrameInstance[spec.getCmpRoleList().size()];

        for (int i = 0; i < spec.getCmpRoleList().size(); i++) {
            ComponentRole       cr  = spec.getCmpRoleList().get(i);
            InterfaceInstance[] iis = new InterfaceInstance[cr.getInterfaces().length];

            for (int j = 0; j < cr.getInterfaces().length; j++) {
                iis[j] = new InterfaceInstance(cr.getName(), cr.getInterfaces()[j].getFcItfName(),
                                               !cr.getInterfaces()[j].isFcClientItf(),
                                               cr.getInterfaces()[j].getMethodNames());
            }

            fis[i] = new FrameInstance(cr.getName(), iis, cr.getBehavior(), i + 1);
        }

        /** the connections in this specification */
        Binding[] bindings = new Binding[spec.getConnectionList().size()];

        for (int k = 0; k < spec.getConnectionList().size(); k++) {
            Connection c = (Connection) spec.getConnectionList().get(k);

            bindings[k] = new Binding(c.getClientCmpRole().getName(), c.getItfClientName(),
                                      c.getServerCompRole().getName(), c.getItfServerName());
        }

        /* Check it ... */
        CheckingResult result = Checker.check(superComponent, fis, bindings);

        System.out.println(result.toString() + "\n");
    }
}
